﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace StarBotDotNet.Tournament1.ZergBattle
{
    public class AttackParty : UnitParty
    {
        Dictionary<StarBotDotNet.UnitType, int> requiredUnits;

        SortedDictionary<int, UnitData> mutaliskPlayer;
        SortedDictionary<int, UnitData> scourgePlayer;

        SortedDictionary<int, UnitData> mutaliskOpponent;
        SortedDictionary<int, UnitData> scourgeOpponent;

        // The third map provides each bot with 12 Mutalisk and 12 Scourge.
        public AttackParty(ProxyBot bot)
        {
            requiredUnits = new Dictionary<UnitType, int>();
            UnitType t = bot.GetCurrentPlayerWorkerType();

            requiredUnits.Add(t, 12);

            mutaliskOpponent = new SortedDictionary<int, UnitData>();
            scourgeOpponent = new SortedDictionary<int, UnitData>();

            mutaliskPlayer = new SortedDictionary<int, UnitData>();
            scourgePlayer = new SortedDictionary<int, UnitData>();
        }

        


        internal override void PopulateWithUnits(ProxyBot bot, UnitDictionary currentUnits, List<UnitData> discoveredUnits, List<UnitData> dissapearedUnits, int currentTime)
        {

            Utilities.WriteColorLine(ConsoleColor.DarkCyan, "TimeStamp = {0}", currentTime);
           
            foreach (UnitData i in discoveredUnits)
            {
                Utilities.WriteColorLine(ConsoleColor.DarkBlue, "PopulateWithUnits : discoveredUnits = {0}", i.ToString());

                if (i.OwnedByPlayer)
                {
                    if (i.Type.Type == Constants.UnitTypes.Zerg_Scourge && scourgePlayer.ContainsKey(i.ID) == false)
                    {
                        scourgePlayer.Add(i.ID, i);
                    }

                    if (i.Type.Type == Constants.UnitTypes.Zerg_Mutalisk && mutaliskPlayer.ContainsKey(i.ID) == false)
                    {
                        mutaliskPlayer.Add(i.ID, i);
                    }
                }
                else // Owned by opponent
                {
                    if (i.Type.Type == Constants.UnitTypes.Zerg_Mutalisk && mutaliskOpponent.ContainsKey(i.ID) == false)
                    {
                        mutaliskOpponent.Add(i.ID, i);
                    }

                    if (i.Type.Type == Constants.UnitTypes.Zerg_Scourge && scourgeOpponent.ContainsKey(i.ID) == false)
                    {
                        scourgeOpponent.Add(i.ID, i);
                    }
                }

            }

            foreach (UnitData i in dissapearedUnits)
            {
                Utilities.WriteColorLine(ConsoleColor.DarkGreen, "PopulateWithUnits : dissapearedUnits = {0}", i.ToString());
                if (i.OwnedByPlayer)
                {
                    if (i.Type.Type == Constants.UnitTypes.Zerg_Scourge && scourgePlayer.ContainsKey(i.ID) == true)
                    {
                        scourgePlayer.Remove(i.ID);
                    }

                    if (i.Type.Type == Constants.UnitTypes.Zerg_Mutalisk && mutaliskPlayer.ContainsKey(i.ID) == true)
                    {
                        mutaliskPlayer.Remove(i.ID);
                        
                    }
                }
                else
                {
                    if (i.Type.Type == Constants.UnitTypes.Zerg_Mutalisk && mutaliskOpponent.ContainsKey(i.ID) == true)
                    {
                        mutaliskOpponent.Remove(i.ID);
                    }

                    if (i.Type.Type == Constants.UnitTypes.Zerg_Scourge && scourgeOpponent.ContainsKey(i.ID) == true)
                    {
                        scourgeOpponent.Remove(i.ID);
                    }
                }

            }

            base.PopulateWithUnits(bot, currentUnits, discoveredUnits, dissapearedUnits, currentTime);
        }

        bool executedMutaliskAttack = false;
        bool executedScourgeAttack = false;
       
        // Current map is 64x64, we start on the x=10; y=64
        System.Drawing.Point center = new System.Drawing.Point(15, 35);
        int Xoffset = 5;
        int Yoffset = -5;
        int counter = 0;
        List<Point> battlePosition = new List<Point>();


        public override void ExecuteTactics(ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {
            if (executedMutaliskAttack == false)
            {
                counter = 0;
                foreach (KeyValuePair<int, UnitData> i in mutaliskPlayer)
                {
                    if (counter == 0)
                    {
                        Utilities.WriteColorLine(ConsoleColor.Red, "Moving MutaliskPlyer ID={0} to ({1}, {2}), counter = {3}", i.Value.ID, center.X, center.Y, counter);
                        battlePosition.Add(new Point(center.X, center.Y));
                        bot.attackMove(i.Value.ID, center.X, center.Y );
                        
                    }
                    else if (counter <= 5)
                    {
                        Utilities.WriteColorLine(ConsoleColor.Green, "Moving MutaliskPlyer ID={0} to ({1}, {2}), counter = {3}", i.Value.ID, center.X + Xoffset, center.Y + Yoffset, counter);
                        battlePosition.Add(new Point(center.X+ 1 + Xoffset, center.Y + Yoffset));
                        bot.attackMove(i.Value.ID, center.X+1 + Xoffset--, center.Y + Yoffset++);
                    } else if (counter >= 6)
                    {
                        Utilities.WriteColorLine(ConsoleColor.Blue, "Moving MutaliskPlyer ID={0} to ({1}, {2}), counter = {3}", i.Value.ID, center.X + Xoffset, center.Y + Yoffset, counter);
                        battlePosition.Add(new Point(center.X+1 + Xoffset, center.Y + Yoffset));
                        bot.attackMove(i.Value.ID, center.X+1 + Xoffset++, center.Y + Yoffset++);
                    }

                    if (counter == 5)
                    {
                        Xoffset = 0;
                        Yoffset = 0;
                    }

                    counter++;
                }

              executedMutaliskAttack = true;
            }

            if (executedScourgeAttack == false)
            {
                counter = 0;
                foreach (KeyValuePair<int, UnitData> i in scourgePlayer)
                {
                    bot.attackMove(i.Value.ID, battlePosition[counter].X+3, battlePosition[counter].Y);
                    counter++;
                }
                executedScourgeAttack = true;
            }
        }

        public override int Priority
        {
            get { return 0; }
        }

        public override Dictionary<UnitType, int> RequiredUnits
        {
            get { return requiredUnits; }
        }

        protected override bool IsUnused(UnitData data)
        {
            return data.IsIdle;
        }
    }
}
